<?php

/***************************************************************\
* FILE:		sys/pages/newsletter.inc
* SECTION:	pages > newsletter
* FUNCTION:	send out newsletters !
* USES:	
* INCLUDES:		
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

include_once('sys/inputcheck.inc');

/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	if (!has_access("NEWSLETTER") && !has_access("ADMIN"))
		errorpage(5);
}

/**
 * appropriate page action interpretations
 */
function page_inc_execute_action($action) {
	global $PAGE;
	switch($action) {
		default:
			break;
	}
}

/**
 * execute page logic
 */
function page_inc_execute() {
	global $PAGE, $CONFIG;
	
	$PAGE['content']['title'] = "Nieuwsbriefjes sturen";

	if ($_POST['confirmed'] == "YES") {
		
		db_do_query(db_query_insert(array('date_sent', 'user_sending', 'subject', 'content', 'fromstring'), array('NOW(14)', $_SESSION['user_id'], db_sqlize($_POST['subject']), db_sqlize($_POST['content']), db_sqlize("{$_POST['from']} &lt;{$_POST['from_mail']}>")), "NEWSLETTER_ARCHIVE"));
		$nl_id = db_insert_id();

		$sql = db_query_select(array(array("user_id", "nick", "email")), array("MEMBER"), ($_POST['ignore_user_pref'] ? 1 : "pref_newsletter = 1"));
		$names = db_do_query($sql);
		
		$numrecipients = 0;
		
		$output = "<p>Opgeslagen in database</p><p>Mail verstuurd naar: \n<ol>\n";
		
		while ($r = db_fetch_array($names)) {
			
			$content = "{$_POST['content']}\n\n\nindien je de nieuwsbrief niet meer wil ontvangen, klik dan onderstaande link\n{$CONFIG['base_url']}{$CONFIG['homedir']}/{$r['user_id']}.unsubscribe?code=" . md5("{$r['nick']}{$r['email']}UNSUBSCRIBE");
		
			if (mail("{$r['nick']} <{$r['email']}>", $_POST['subject'], $content, "Reply-To: {$_POST['from_mail']}\r\nReturn-Path: {$_POST['from_mail']}\r\nFrom: {$_POST['from']} <{$_POST['from_mail']}>")) {
				$output .= "<li>{$r['nick']} ({$r['email']})\n";
				$numrecipients++;
			} else {
				$output .= "<li>SENDING FAILED for {$r['nick']} ({$r['email']})\n";
			}
		}
		
		db_do_query(db_query_update(array('date_sent', 'recipients'), array('date_sent', $numrecipients), "NEWSLETTER_ARCHIVE", "newsletter_id = $nl_id"));
		
		endpage($output . "</ol>");

	} else if ($_POST['submitted']) {
		$fields = array();
		$fields[] = form_input_hidden("ignore_user_pref", $_POST["ignore_user_pref"] ? 1 : 0);
		$fields[] = form_input_field("Afzender email", "from_mail", $_POST["from_mail"], 0, "", "", 1);
		$fields[] = form_input_field("Afzender", "from", $_POST["from"], 0, "", "", 1);
		$fields[] = form_input_text("Inhoud mail", "content", stripslashes($_POST["content"]), 0, 0, "", 1);
		$fields[] = form_input_field("Onderwerp", "subject", stripslashes($_POST["subject"]), 0, "", "", 1);
		$fields[] = form_input_radio("&nbsp;", 'confirmed', array('YES', 'NO'), array('ja', 'nee'));
		$fields[] = form_info("Ben je zeker dat je deze nieuwsbrief wil versturen naar alle leden" . ($_POST['ignore_user_pref'] ? ", inclusief wie volgens zijn of haar profiel geen nieuwsbrief wil ontvangen" : ", behalve wie in zijn of haar profiel heeft gekozen om geen nieuwsbrief te ontvangen") . "?");
		$PAGE['content']['form'] = form_create("confirmform", $fields);

	} else {
	
		$inputs = array();
		$inputs[] = form_input_checkbox("Negeer user settings voor het ontvangen van nieuwsbrieven (stuurt maw naar iedereen)", "ignore_user_pref");
		$inputs[] = form_input_field("Afzender email", "from_mail", $_SESSION['member']['email']);
		$inputs[] = form_input_field("Afzender", "from", $CONFIG['mail_from']);
		$inputs[] = form_info("Gebruik geen html codes in de inhoud van de nieuwsbrief of de subject line, want die worden vaak niet of niet correct weergegeven in email clients of webmail clients");
		$inputs[] = form_input_text("Inhoud mail", "content", "blablabla");
		$inputs[] = form_input_field("Onderwerp", "subject", "Mandragon nieuwsbrief ###");
		
		$PAGE['content']['form'] = form_create("nlform", $inputs);
		
	}
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE;
	print_form($PAGE['content']['form']);
}

?>